package basic.study.leetcode;

import java.util.PriorityQueue;

/**
 * @ClassName Mid33
 * @Description 搜索旋转排序数组
 * @Company inspur
 * @Author Kevin
 * @Date 2020/6/19 10:09
 * @Version 1.0
 */
public class Mid33 {
    static class Solution {
        public int search(int[] nums, int target) {
            int start = 0;
            int end = nums.length - 1;

            while (start <= end) {
                int mid = start + ((end -start) >> 1);
                if (nums[mid] == target) {
                    return mid;
                }
                //判断哪一边有序
                if (nums[start] <= nums[mid]) {
                    if (target >= nums[start] && target < nums[mid]) {
                        end = mid - 1;
                    } else {
                        start = mid + 1;
                    }
                } else {
                    if (target > nums[mid] && target <= nums[end]) {
                        start = mid + 1;
                    } else {
                        end = mid - 1;
                    }
                }
            }
            return -1;
        }
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.search(new int[]{4,5,6,7,0,1,2}, 0));
    }
}
